package com.ljt.business.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.business.entity.Product;
import com.ljt.business.service.ProductService;
import com.ljt.business.vo.ProductVO;
import com.ljt.response.Result;
import com.ljt.utils.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * <p>
 * 物资表 前端控制器
 * </p>
 *
 * @author ljt
 * @since 2021-07-02
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @ApiOperation(value="物资信息",notes = "分页模糊查询所有物资信息")
    @GetMapping("queryAllProductByCondition")
    public Result queryAllProductByCondition(
            @RequestParam(required = false,defaultValue = "1") Integer current,
            @RequestParam(required = false,defaultValue = "6") Integer size,
            @RequestParam(required = false) String name){

        Page<Product> productPage = new Page<>(current, size);
        productPage = productService.queryAllProductByCondition(productPage, name);
        return Result.ok().data("productPage", productPage);

    }

    /**
     * 分页查询所有物资信息
     * @param current 页码
     * @param size 每页展示多少条数据
     * @return
     */

    @ApiOperation(value="物资信息",notes = "分页查询所有物资信息")
    @GetMapping("queryAllProductByPage")
    public Result queryAllProductByPage(@RequestParam(required = false,defaultValue = "1") Integer current,
                                        @RequestParam(required = false,defaultValue = "6") Integer size){
        Page<Product> productPage=new Page<>(current,size);
        productPage=productService.page(productPage);
        return Result.ok().data("productPage",productPage);
    }

    @ApiOperation(value = "获取带有类别名的物资信息",notes = "根据表中的一级、二级、三级分类编号来查询物资信息")
    @PostMapping("/getProductWithCategoryName")
    public Result getProductWithCategoryName(
            @RequestParam(required = false,defaultValue = "1")Integer current,
            @RequestParam(required = false,defaultValue = "6")Integer size,
            @RequestBody(required = false) ProductVO productVO){
        Page<Product> page = new Page<>();
        page.setSize(size).setCurrent(current);
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtil.isEmpty(productVO)) {
            if (!ObjectUtil.isEmpty(productVO.getName())) {
                queryWrapper.like("name",productVO.getName());
            }
            if(!ObjectUtil.isEmpty(productVO.getCategoryIds()) && productVO.getCategoryIds().size() > 0){
                queryWrapper.or().in("one_category_id",productVO.getCategoryIds())
                        .or().in("two_category_id",productVO.getCategoryIds())
                        .or().in("three_category_id",productVO.getCategoryIds());
            }
        }

        IPage<Product> productIPage = productService.queryProductWithCategoryName(page, queryWrapper);
        return Result.ok().data("productIPage",productIPage);
    }


    @ApiOperation(value = "物资信息",notes = "添加物资信息")
    @GetMapping("insertProduct")
    public Result insertProduct(Product product){
        boolean i=productService.save(product);
        if(i){
            return Result.ok().data("product",product);
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value = "物资信息",notes = "修改物资信息")
    @GetMapping("updateProductById")
    public Result updateProductById(Product product){
        boolean i=productService.updateById(product);
        if(i){
            return Result.ok().data("product",product);
        }else{
            return Result.error();
        }
    }
    @ApiOperation(value="物资信息表",notes = "根据id查询单个物资信息")
    @GetMapping("queryProductById")
    public Result queryProductById(int id){
        Product product=productService.getById(id);
        return Result.ok().data("product",product);
    }

    @ApiOperation(value = "导出",produces="application/octet-stream")
    @GetMapping("exportProductToExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<Product> productList = productService.list();
        ExcelUtil<Product> util = new ExcelUtil<Product>(Product.class);
        util.exportExcel(response,productList , "商品数据");
    }

    @ApiOperation("导入")
    @PostMapping("importProductToExcel")
    public Result importExcel(@RequestBody MultipartFile file) throws Exception {
        ExcelUtil<Product> util = new ExcelUtil<>(Product.class);
        List<Product> productList = util.importExcel(file.getInputStream());
        boolean insertFlag = productService.saveBatch(productList);
        if (insertFlag){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}
